--#include "data\config\item\SwingEquip.lua" once
--#include "data\functions\ActorEvent\Utils.lua" once
--#include "data\functions\Activity\OpenServer\OpenServerSports.lua" once
--#include "data\functions\Activity\Operation\operation.lua" once   --运营活动
--#include "data\functions\Activity\OpenServer\OpenServerPKRank.lua" once   

--下发翅膀操作结果

-- 消耗成功扣除羽毛或元宝
function isConsumeSuccess(sysarg, bYBBuy, consume, itemCount)
	if itemCount >= consume.count then
		local delCount = Actor.removeItem(sysarg, consume.id, consume.count)
		if delCount <= 0 then
			return false
		end
	else
		-- 是否使用元宝
		if bYBBuy == 0 then
			Actor.sendItemLack( sysarg, qatEquipment, consume.id, consume.count )
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00003, ttFlyTip)
			return false
		end
	
		local needYbCount =  (consume.count - itemCount) * consume.yb
		if Actor.changeMoney(sysarg, mtYuanbao, -needYbCount, GameLog.clMoneyCostMaterial, Lang.LogTips.s00001) == false then
			Actor.sendItemLack( sysarg, qatYuanbao, 0, needYbCount)
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.zf063, ttFlyTip)
			return false
		elseif itemCount > 0 then
			Actor.removeItem(sysarg, consume.id, itemCount)
		end
	end
	return true
end

-- 下发进阶信息
function sendSwingData(sysarg, nResult)
	--[[
	local netPack = DataPack.allocPacket(sysarg, 52, 1)
	if not netPack then
		return
	end
	
	DataPack.writeByte(netPack, nResult)		-- 1成功, 0失败
	DataPack.flush(netPack)
	]]
end

-- 进阶翅膀装备
function UpSwingEquip(sysarg, bYBBuy)
	local swingEquipId = Actor.getUIntProperty(sysarg, PROP_ACTOR_SWINGID)
	if swingEquipId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00001, ttFlyTip)
		return
	end
	
	local equipCfg = SwingEquipConfig.SwingEquipTable[swingEquipId]
	local nextEquipCfg = SwingEquipConfig.SwingEquipTable[swingEquipId + 1]

	-- 配置出错
	if not equipCfg then
		print("config error, UpSwingEquip() SwingEquip[" .. swingEquipId .. "]不存在!")
		return
	end
	
	-- 不能再进阶了
	if not nextEquipCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00002, ttFlyTip)
		return
	end
	
	-- 扣除羽毛或元宝
	local itemCount = Actor.getItemCount(sysarg, equipCfg.consume.id)
	if isConsumeSuccess(sysarg, bYBBuy, equipCfg.consume, itemCount) == false then
		return
	end

	-- 祝福值
	local curValue = Actor.getUIntProperty(sysarg, PROP_ACTOR_SWING_EXP) or 0
	curValue = curValue + equipCfg.addValue
	local index = #equipCfg.upRange
	for k, v in ipairs(equipCfg.upRange) do 
		if curValue < v[1] then
			index = k - 1
			break
		end
	end

	local upResult = 0
	if math.random(10000) <= equipCfg.upRange[index][2] then
		upResult = 1
	end

	if upResult > 0 then
		curValue = 0
		Actor.setUIntProperty(sysarg, PROP_ACTOR_SWINGID, nextEquipCfg.id)
		Actor.collectOperate(sysarg, 0)											-- 属性
		Actor.collectOperate(sysarg, 1)											-- 外观
		Actor.triggerQuestEvent(sysarg, 42, 0, nextEquipCfg.id, false)			-- 任务
		Actor.triggerAchieveEvent(sysarg, 106, nextEquipCfg.id)					-- 成就
		System.rankingUpdateValue(5, sysarg, nextEquipCfg.id, equipCfg.id)		-- 排行榜

		local oldname = equipCfg.name
		local newname = nextEquipCfg.name
		local ActorId = Actor.getIntProperty(sysarg,PROP_ENTITY_ID)
		local ActorName = Actor.getName(sysarg)
		-- 日志
		System.sendCommonLog(GameLog.cWingLevel, ActorId, ActorName, equipCfg.id, nextEquipCfg.id, 0, "", oldname, Lang.LogTips.s00002, "", newname)
		-- 提示
		local tipmsg = string.format(Lang.ScriptTips.s00005, newname)
		Actor.sendTipmsg(sysarg, tipmsg, ttFlyTip)
		-- 广播
		if nextEquipCfg.isBroad then
			local sysMsg = string.format(Lang.ScriptTips.s00006, ActorName, oldname, newname)
			System.broadcastTipmsg(sysMsg, ttChatWindow + ttScreenCenter)
		end

		OpenServerTriggleEvent(sysarg, enOpenServerId_Swing, nextEquipCfg.id, 0)   --开服活动翅膀竞技
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Swing, nextEquipCfg.id, 0)
		OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_Swing_2, nextEquipCfg.id)
	end

	Actor.setUIntProperty(sysarg, PROP_ACTOR_SWING_EXP, curValue)
	Actor.updateActorEntityProp(sysarg)
	sendSwingData(sysarg, upResult)

	swingLuckyOperationChangeHandler(sysarg, equipCfg.addValue)	--翅膀祝福值竞技
end


-- 激活翅膀
function activeSwing(sysarg)
	local swingId = Actor.getUIntProperty(sysarg, PROP_ACTOR_SWINGID)	
	if swingId > 0 then	
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00007, ttFlyTip)
		return
	end
	
	-- 等级判断
	if SwingEquipConfig.givingSwing ~= nil and not Actor.checkActorLevel(sysarg, SwingEquipConfig.givingSwing, 0) then
		--Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.s00008, SwingEquipConfig.givingSwing ), ttFlyTip)
		return
	end
	
	-- 激活
	local config = SwingEquipConfig.SwingEquipTable
	local newSwingId = config[1].id
	Actor.setUIntProperty(sysarg, PROP_ACTOR_SWING_EXP, 0)								-- 祝福值
	Actor.setUIntProperty(sysarg, PROP_ACTOR_SWINGID, newSwingId)						-- 翅膀id
	Actor.collectOperate(sysarg, 0)														-- 属性
	Actor.collectOperate(sysarg, 1)														-- 外观
	
	Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00009, ttFlyTip)							-- 提示
	Actor.triggerQuestEvent(sysarg, 42, 0, 1, false)									-- 任务
	Actor.triggerAchieveEvent(sysarg, 106, 1)											-- 成就
end

function OnSwingNewDaySixClock(sysarg)
	local swingID = Actor.getUIntProperty(sysarg, PROP_ACTOR_SWINGID)
	if swingID >= SwingEquipConfig.clearLevel then
		local swingExp = Actor.getUIntProperty(sysarg, PROP_ACTOR_SWING_EXP)
		if swingExp > 0 then	
			Actor.setUIntProperty(sysarg, PROP_ACTOR_SWING_EXP, 0)
			local actorId = Actor.getActorId(sysarg)
			local title = Lang.ScriptTips.s00010
			local content = Lang.ScriptTips.s00011
			Awards.GiveToMailById(actorId, {}, title, content) 
		end
	end
	swingID =  Hero.getHeroProperty(sysarg, -1, enHeroPropSwingLevel)
	if swingID >= HeroSwingEquipConfig.clearLevel then
		local swingExp = Hero.getHeroProperty(sysarg, -1, enHeroPropSwingExp)
		if swingExp > 0 then	
			Hero.setHeroProperty(sysarg, -1, enHeroPropSwingExp, 0)
			local actorId = Actor.getActorId(sysarg)
			local title = Lang.ScriptTips.s00012
			local content = Lang.ScriptTips.s00013
			Awards.GiveToMailById(actorId, {}, title, content) 
		end
	end
end
function UpHeroSwing(sysarg, bYBBuy)
	local swingEquipId = Hero.getHeroProperty(sysarg, -1, enHeroPropSwingLevel)
	if swingEquipId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00001, ttFlyTip)
		return
	end
	
	local equipCfg = HeroSwingEquipConfig.SwingEquipTable[swingEquipId]
	local nextEquipCfg = HeroSwingEquipConfig.SwingEquipTable[swingEquipId + 1]

	-- 配置出错
	if not equipCfg then
		print("config error, HeroSwingEquipConfig() SwingEquip[" .. swingEquipId .. "]不存在!")
		return
	end
	
	-- 不能再进阶了
	if not nextEquipCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.s00002, ttFlyTip)
		return
	end
	local logId,logStr = LogIds.HeroSwingLogId,Lang.LogTips.s00003
	local equipCfgConsume = table.deepcopy(equipCfg.consume)
	local consume = {equipCfgConsume}
	if CheckConsumeCondNoTip(sysarg, consume, 1) then
		if not DoConsumeCond( sysarg, consume, 1, logId,logStr, false ) then
			return
		end
	else
		if bYBBuy == 0 then
			Actor.sendItemLack( sysarg, equipCfgConsume.type, equipCfgConsume.id, equipCfgConsume.count)
			return false
		else
			equipCfgConsume.type = qatYuanbao
			equipCfgConsume.count = equipCfgConsume.yb
			consume = {equipCfgConsume}
			if not DoConsumeCond( sysarg, consume, 1, logId,logStr, false ) then
				return
			end
		end
	end
	
	-- 祝福值
	local curValue = Hero.getHeroProperty(sysarg, -1, enHeroPropSwingExp)
	curValue = curValue + equipCfg.addValue
	local index = #equipCfg.upRange
	for k, v in ipairs(equipCfg.upRange) do 
		if curValue < v[1] then
			index = k - 1
			break
		end
	end

	local upResult = 0
	if math.random(10000) <= equipCfg.upRange[index][2] then
		upResult = 1
	end

	if upResult > 0 then
		curValue = 0
		Hero.setHeroProperty(sysarg, -1, enHeroPropSwingLevel, nextEquipCfg.id)
		
		local oldname = equipCfg.name
		local newname = nextEquipCfg.name
		local ActorId = Actor.getIntProperty(sysarg,PROP_ENTITY_ID)
		local ActorName = Actor.getName(sysarg)
		-- 日志
		System.sendCommonLog(LogIds.HeroSwingLogId, ActorId, ActorName, equipCfg.id, nextEquipCfg.id, 0, "", oldname, Lang.LogTips.s00003, "", newname)
		-- 提示
		local tipmsg = string.format(Lang.ScriptTips.s00005, newname)
		Actor.sendTipmsg(sysarg, tipmsg, ttFlyTip)
		-- 广播
		if nextEquipCfg.isBroad then
			local sysMsg = string.format(Lang.ScriptTips.s00006, ActorName, oldname, newname)
			System.broadcastTipmsg(sysMsg, ttChatWindow + ttScreenCenter)
		end

	end
	Hero.setHeroProperty(sysarg, -1, enHeroPropSwingExp, curValue)
end
function OnSwingEquipLogin(sysarg)

end

function OnHeroCircleHandle(sysarg,circle)
	if circle >= HeroSwingEquipConfig.givingSwingCircle then
		if Hero.getHeroProperty(sysarg, -1, enHeroPropSwingLevel) == 0 then
			Hero.setHeroProperty(sysarg, -1, enHeroPropSwingLevel, 1)
		end
	end
end


--翅膀升阶函数接口
EventCallDispatcher.handleSwingEquipOp = function (sysarg, args)
	if not args then 
		return 
	end
	local opType = args[1]
	local bYBBuy = args[2] or 0
	if opType == 1 then
		activeSwing(sysarg)
	elseif opType == 2 then
		UpSwingEquip(sysarg, bYBBuy)
	elseif opType == 3 then
		UpHeroSwing(sysarg, bYBBuy)
	end
end

--注册回调函数	
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeSwingOp, EventCallDispatcher.handleSwingEquipOp) 
